<?php
class MailaddressesController extends AppController {

	var $name = 'Mailaddresses';
	var $uses = array('Mailaddress', 'User', 'Transact');
	var $scaffold;
	var $helpers = array('Html', 'Form');
	var $protectedActions = array('index', 'add', 'edit', 'delete', 'view');
	var $components = array('RequestHandler', 'AdminAuth', 'Pmaimai', 'Token');
	
	function activate()
	{
		$this->Session->delete($this->sessionUserName);
		$this->set('error', false);
		//GETのデータはPOSTに入れちゃう　入力エラー再表示時対策
		if(empty($this->data['Mailaddress'])){
			$this->data['Mailaddress'] = array(
				'id' => $this->params['pass'][0],
				'token' => $this->params['pass'][1],
			);
		}
		
		//仮パスワードの照合ここから
		if(empty($this->data['Mailaddress']['id']) || empty($this->data['Mailaddress']['token'])){
			$this->redirect('/');
		}
		
		$this->Mailaddress->unbindFully();
		$mail = $this->Mailaddress->findById($this->data['Mailaddress']['id']);
		if(!$mail){
			$this->redirect('/');
		}
		$this->set('mail', $mail);
		$this->Mailaddress->set($mail);
		
		if(!$this->Mailaddress->checkActivate($this->data['Mailaddress']['token'])){
			$this->flash(current($this->Mailaddress->validationErrors) , '/');
			return;
		}
		//仮パスワードの照合ここまで
		
		$bindusers = array(
			'hasOne' => array(
				'Mailaddress' =>  $this->User->hasOne_active['Mailaddress'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		$user = $this->User->findById($mail['Mailaddress']['user_id']);
		if(!$user){
			$this->redirect('/');
		}
		$this->set('user', $user);
		
		if(!isset($this->data['Mailaddress']['password'])){
			return;
		}
		
		if(empty($this->data['Mailaddress']['password'])){
			$this->_myErrorMsg[] = 'パスワードが未入力です';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->User->set($user);
		if(!$this->User->isAuth($this->data['Mailaddress']['password'])){
			$this->_myErrorMsg[] = 'そんなユーザーいません';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->begin();
		
		$result = true;
		
		//古い方のメールをディスアクティベート
		$oldMail = new Mailaddress();
		$oldMail->set($user);
		$oldMail->updateStatus('disactivate') or $result = false;
		
		//新しい方のメールをアクティベート
		$this->Mailaddress->updateStatus('activate') or $result = false;
		
		if(!$result){
			$this->Transact->rollback();
			$this->_myErrorMsg[] = 'なんかエラー';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$sendData = array();
		$sendData[] = array(
			'mailaddress' => $user['Mailaddress']['mailaddress'],
			'viewpath' => '/email/mailaddress_activate_old',
			'subject' => '【pmaimai】メールアドレス変更完了のお知らせ',
		);
		$sendData[] = array(
			'mailaddress' => $mail['Mailaddress']['mailaddress'],
			'viewpath' => '/email/mailaddress_activate_new',
			'subject' => '【pmaimai】メールアドレス変更完了のお知らせ',
		);
		
		if(!$this->_sendMail($sendData)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->commit();
		$this->Session->write($this->sessionUserName, $mail['Mailaddress']['user_id']);
		$this->flash('アクティベート完了しました','/');
		
	}
	
	function change()
	{
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$bindusers = array(
			'hasOne' => array(
				'Mailaddress' =>  $this->User->hasOne_active['Mailaddress'],
			),
		);
		$this->User->bindModel($bindusers);
		$this->User->recursive = 1;
		
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		$this->set('error', false);
		
		if(!$this->data){
			return;
		}
		
		$result = true;
		if($this->data['Mailaddress']['mailaddress'] !== $this->data['Mailaddress']['confirmmail']){
			$this->_myErrorMsg[] = '入力と再入力のメールアドレスが違います';
			$result = false;
		}
		if(empty($this->data['Mailaddress']['mailaddress']) || empty($this->data['Mailaddress']['confirmmail'])){
			$this->_myErrorMsg[] = 'メールアドレス未入力の箇所があります';
			$result = false;
		}
		if(empty($this->data['Mailaddress']['password'])){
			$this->_myErrorMsg[] = 'パスワードが未入力です';
			$result = false;
		}
		
		if(!$result){
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->User->set($user);
		if(!$this->User->isAuth($this->data['Mailaddress']['password'])){
			$this->_myErrorMsg[] = 'えらー';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$mail = $this->Mailaddress->register($userID, $this->data['Mailaddress']['mailaddress']);
		if(!$mail){
			$this->set('error', true);
			$this->set('errorMsg', $this->Mailaddress->validationErrors);
			return;
		}
		$mailID = $this->Mailaddress->getLastInsertID();
		$this->set('mailID', $mailID);
		$this->set('mail', $mail);
		
		$sendData = array();
		$sendData[] = array(
			'mailaddress' => $user['Mailaddress']['mailaddress'],
			'viewpath' => '/email/mailaddress_change_old',
			'subject' => '【pmaimai】メールアドレス変更確認のお知らせ',
		);
		$sendData[] = array(
			'mailaddress' => $mail['Mailaddress']['mailaddress'],
			'viewpath' => '/email/mailaddress_change_new',
			'subject' => '【pmaimai】メールアドレス変更確認のお知らせ',
		);
		
		if(!$this->_sendMail($sendData)){
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Session->delete($this->sessionUserName);
		$this->flash("確認メールを送信しました", '/');
	}
	
	
	
	

}
?>